﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.Mail;
using System.Configuration;

namespace CRM.Common
{
    public class SendMailHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="receiver">接收人Email地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <returns></returns>
        public bool SendMail(string receiver, string subject, string body)
        {
            return SendMail(receiver, subject, body, true);
        }
        /// <summary>
        /// 发送带Html标签内容的邮件内容
        /// </summary>
        /// <param name="receiver">接收人Email地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="isBodyHtml">内容是否包含Html标签</param>
        /// <returns></returns>
        public bool SendMail(string receiver, string subject, string body, bool isBodyHtml)
        {
            return SendMail(receiver, subject, body, isBodyHtml, string.Empty);
        }

        /// <summary>
        /// 发送带附件的邮件
        /// </summary>
        /// <param name="receiver">接收人Email地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="isBodyHtml">内容是否包含Html标签</param>
        /// <param name="filePath">附件的文件路径</param>
        /// <returns></returns>
        public bool SendMail(string receiver, string subject, string body, bool isBodyHtml, string filePath)
        {
            string mailSender = Configration.GetAppSetting("mailSender");
            string mailPwd = Configration.GetAppSetting("mailPwd");
            string mailServer = Configration.GetAppSetting("mailServer");

            try
            {
                string[] receiverList = receiver.Split(';');

                MailMessage message = new MailMessage();

                message.To = receiver;

                message.From = mailSender;
                message.Subject = subject;
                message.Body = body;

                if (!string.IsNullOrEmpty(filePath))
                {
                    MailAttachment attach = new MailAttachment(filePath);
                    message.Attachments.Add(attach);
                }
                //message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = "1";
                //message.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "";
                //message.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "";
                //if (!string.IsNullOrEmpty(filePath))
                //    message.Attachments.Add(new Attachment(filePath));

                //SmtpClient mailClient = new SmtpClient(mailServer);

                //mailClient.Credentials = new System.Net.NetworkCredential(mailSender, mailPwd);
                //mailClient.Send(message);     //发送邮件
                SmtpMail.Send(message);
            }
            catch (Exception ex)
            {
                // 如果邮件发送失败，可能只是部分邮件发送失败！

                LogHelper.WriteLog("", ex);
                return false;
            }
            return true;
        }

    }
}
